﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.OleDb;


namespace WindowsFormsApplication3
{
    
    public partial class Purchase_RequistitonForm : Form
    {
       // private String connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\NauferTransactionDemoDB.accdb";
        DataBean.PurchaseRequestDatabean purchaserequestdatabean = null;

        public Purchase_RequistitonForm()
        {
            InitializeComponent();
            tblpurchaserequest.RowCount = 1;

            newprnumget();

        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
           
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            validateDatagrid();
        }
        /// <summary>
        /// validates the control
        /// </summary>
        public void validateDatagrid()
        {

         Boolean sucess  = false;

       //if textbox is not autofilled with prno
            
            if (txtprno.Text.Trim() == null || txtprno.Text.Trim() == "")
         {
             tblpurchaserequest.RowCount = 1;
             MessageBox.Show("Error in Auto Generating PRNO# Please Restart the Application");
         }
           
            
           // only one row is present means no alteration done 
            if (tblpurchaserequest.RowCount == 1)
            {
                lblstatus.Text = "There is No  Purchase Data to Add";

            }
            else
            {
                
                for (int i = 0; i < tblpurchaserequest.RowCount-1; i++)
                {

                    lblstatus.Text = "";

                    if ( tblpurchaserequest.Rows[i].Cells[0].Value == null ||tblpurchaserequest.Rows[i].Cells[0].Value.ToString().Trim() == "" )
                    {
                        
                        lblstatus.Text = "Please Enter Item in " + i + "th ROW";
                    }

                    else if (tblpurchaserequest.Rows[i].Cells[1].Value == null || tblpurchaserequest.Rows[i].Cells[1].Value.ToString().Trim() == "")
                    {
                        lblstatus.Text = "Please Enter Description in " + i + "th ROW";
                    }
                    else if (tblpurchaserequest.Rows[i].Cells[2].Value == null || tblpurchaserequest.Rows[i].Cells[2].Value.ToString().Trim() == "")
                    {
                        lblstatus.Text = "Please Enter Supplier in " + i + "th ROW";
                    }
                    else if (tblpurchaserequest.Rows[i].Cells[3].Value == null || tblpurchaserequest.Rows[i].Cells[3].Value.ToString().Trim() == "")
                    {
                        lblstatus.Text = "Please Enter Quantity in " + i + "th ROW";
                    }
                    else
                    {
                       sucess= true;
                    }
                }
                if (sucess== true)
                {
                    insertdatabean();
                    
                   
                }

            }
            


        }
        
        ///<summary>
        /// insertdata to a databean 
        /// </summary>
        public void insertdatabean()
        {

            if (tblpurchaserequest.RowCount == 1)
            {
                lblstatus.Text = "There is No  Purchase Data to Add";

            }
            else
            {
                  for (int i = 0; i < tblpurchaserequest.RowCount - 1; i++)
                {
                    purchaserequestdatabean = new DataBean.PurchaseRequestDatabean();
                   
                    purchaserequestdatabean.Prno = txtprno.Text;
                    purchaserequestdatabean.Item = tblpurchaserequest.Rows[i].Cells[0].Value.ToString();
                    purchaserequestdatabean.Description = tblpurchaserequest.Rows[i].Cells[1].Value.ToString();
                    purchaserequestdatabean.Supplier = tblpurchaserequest.Rows[i].Cells[2].Value.ToString();
                    purchaserequestdatabean.Quantity = tblpurchaserequest.Rows[i].Cells[3].Value.ToString();
                   
                      purchaserequestdatabean.Remark = tblpurchaserequest.Rows[i].Cells[4].Value.ToString();
                      purchaserequestdatabean.Prdate = DateTime.Now.Date;
                      insertintDBppurchaserequest(purchaserequestdatabean);
                      
                      
                  }
                  MessageBox.Show("sucess");
                  newprnumget();
                  tblpurchaserequest.RowCount = 1;
            }
        }



        /// <summary>
        /// inser into database table
        /// </summary>
        /// <param name="purchaserequestdatabean"></param>
        public void insertintDBppurchaserequest(DataBean.PurchaseRequestDatabean purchaserequestdatabean)
        {



            try
            {

                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(Program.ConnStr);
                oleDbConnection1.Open();

                OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO purchasereportmastertable (prnum ,item,description,quantity,supplier,remark,prdate) Values ('" + purchaserequestdatabean.Prno.ToString() + "','" + purchaserequestdatabean.Item.ToString() + "','" + purchaserequestdatabean.Description + "','" + purchaserequestdatabean.Quantity + "','" + purchaserequestdatabean.Supplier + "','" + purchaserequestdatabean.Remark + "','" + purchaserequestdatabean.Prdate + "') ", oleDbConnection1);
                oleDbCommand1.CommandType = CommandType.Text;
                oleDbCommand1.ExecuteNonQuery();

                oleDbConnection1.Close();
               
            }
            catch (Exception e)
            {

                MessageBox.Show("Exception at adding  new PR Database close any Access Database program and restart" + e);
               
            }
            





        }

         /// <summary>
        /// automatically set a pr# when window open
        /// </summary>

        public void newprnumget()
        {
            try
            {
                String jobcodeno = null;
                Int32 S = 0;
                txtprno.Text = "";
                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(Program.ConnStr);
                oleDbConnection1.Open();


                String query = "SELECT TOP 1 prpk  FROM purchasereportmastertable  ORDER BY prpk DESC";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, Program.ConnStr);


                DataSet ds = new DataSet();

                dAdapter.Fill(ds, "tblpurchase");
                if (ds.Tables[0].Rows.Count == 0)
                {
                    S = 0;
                }
                else
                {
                    S = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                }

                S++;

                jobcodeno = "NPR#" + S.ToString();

                txtprno.Text = "PR#" + S.ToString();
                txtprno.Enabled = false;
            }
            catch (Exception exp)
            {
                
                MessageBox.Show("Failed To AutoGenerate PR# "+ exp+"");
            }

        }










        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
